package com.it.schoolbookshop_back.config;

import com.it.schoolbookshop_back.websocket.ChatWebSocketHandler;
import com.it.schoolbookshop_back.websocket.NotificationWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置类
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private ChatWebSocketHandler chatWebSocketHandler;
    
    @Autowired
    private NotificationWebSocketHandler notificationWebSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 聊天WebSocket处理器
        registry.addHandler(chatWebSocketHandler, "/ws/chat")
                .setAllowedOrigins("http://localhost:5173");
                
        // 通知WebSocket处理器 - 注意这里使用notificationWebSocketHandler而不是chatWebSocketHandler
        registry.addHandler(notificationWebSocketHandler, "/ws/notifications/{userId}")
                .setAllowedOrigins("http://localhost:5173");
    }
} 